/**
 * 
 */
package com.shine.icis.dao.imp;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.sql.hibernate.CommQuery;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.dao.ICompanyInfoDAO;
import com.shine.icis.dao.imp.BaseHibernateDAO;
import com.shine.icis.dto.CompanyInfo;

/**
 * @author zsy
 *
 */
public class CompanyInfoDAO extends BaseHibernateDAO implements ICompanyInfoDAO {

	@Override
	public void save(CompanyInfo info) {
		Query query = getSession().getNamedQuery("CompanyInfoDAO.save");
		query.setString("companyCode", info.getCompanyCode());
		query.setString("companyName", info.getCompanyName());
		query.setString("settleInvoCode", info.getSettleInvoCode());
		query.setString("ofSaleInvoCode", info.getOfSaleInvoCode());
		query.setString("settleInvoID", info.getSettleInvoID());
		query.setString("settleInvoAcctNO", info.getSettleInvoAcctNO());
		query.setString("occurDate", info.getOccurDate());
		query.setLong("recordNo", info.getRecordNo());
		query.executeUpdate();
	}

	@Override
	public void update(String oldCompanyCode, CompanyInfo info) {
		Query query = getSession().getNamedQuery("CompanyInfoDAO.update");
		query.setString("companyCode", info.getCompanyCode());
		query.setString("companyName", info.getCompanyName());
		query.setString("settleInvoCode", info.getSettleInvoCode());
		query.setString("ofSaleInvoCode", info.getOfSaleInvoCode());
		query.setString("settleInvoID", info.getSettleInvoID());
		query.setString("settleInvoAcctNO", info.getSettleInvoAcctNO());
		query.setString("occurDate", info.getOccurDate());
		query.setString("oldCompanyCode", oldCompanyCode);
		query.executeUpdate();
	}
	
	@Override
	public void delete(String companyCode) {
		Query query = getSession().getNamedQuery("CompanyInfoDAO.delete");
		query.setString("companyCode", companyCode);
		query.executeUpdate();
		
	}
	
	@Override
	public boolean checkId(String companyCode) {
		Query query = getSession().getNamedQuery("CompanyInfoDAO.checkId");
		query.setString("companyCode", companyCode);
		Long count = (Long)query.uniqueResult();
		if (count != null && count.longValue() > 0) {
			return false;
		}
		return true;
	}
	
	@Override
	public long findNextRecordNo() {
		Query query = getSession().getNamedQuery("CompanyInfoDAO.findNextRecordNo");
		Long count = (Long)query.uniqueResult();
		if (count == null) {
			return 1;
		}
		return count.longValue() + 1;
	}
	
	@Override
	public long findTotalCount() {
		Query query = getSession().getNamedQuery("CompanyInfoDAO.findTotalCount");
		Long count = (Long)query.uniqueResult();
		return count == null ? 0 : count.longValue();
	}

	@Override
	public ListPage<CompanyInfo> findListPage(QueryParameters qp) {
		try {
			Query query = getSession().getNamedQuery("CompanyInfoDAO.findListPage");
			Query countQuery = getSession().getNamedQuery("CompanyInfoDAO.findListPage_count");
			ListPage<Object> page = new CommQuery<Object>().queryListPage(qp, query, countQuery);
			ListPage<CompanyInfo> comPage = new ListPage<CompanyInfo>();
			comPage.setTotalCount(page.getTotalCount());
			comPage.setCurrentPageNo(page.getCurrentPageNo());
			comPage.setCurrentPageSize(page.getCurrentPageSize());
			if (page != null && page.getDataList() != null) {
				List<CompanyInfo> datas = new ArrayList<CompanyInfo>();
				for (Object o : page.getDataList()) {
					Object[] oArr = (Object[])o;
					CompanyInfo com = new CompanyInfo();
					com.setCompanyCode(String.valueOf(oArr[0]));
					com.setCompanyName(String.valueOf(oArr[1]));
					com.setSettleInvoCode(String.valueOf(oArr[2]));
					com.setOfSaleInvoCode(String.valueOf(oArr[3]));
					com.setSettleInvoID(String.valueOf(oArr[4]));
					com.setSettleInvoAcctNO(String.valueOf(oArr[5]));
					com.setOccurDate(String.valueOf(oArr[6]));
					datas.add(com);
				}
				comPage.setDataList(datas);
			}
			return comPage;
		} catch(RuntimeException re) {
			re.printStackTrace();
			return new  ListPage<CompanyInfo>();
		}
	}
}
